/*
 * $Id$
 */
package corejs.rt;

import corejs.stmt.Block;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Function extends Invocable {
   
   private ActivationRecord _parentAR;
   private String _name;
   private String[] _argNames;
   private Block _body;

   public Function(final Value proto) {
      //super(proto);
   }
   
   /**
    *
    * @param ar
    * @param name
    * @param argNames
    * @param body
    */
   public Function(final ActivationRecord ar, final String name,
                   final String[] argNames, final Block body) {
      _parentAR = ar;
      _name = name;
      _argNames = argNames;
      _body = body;
      store("name", new Str(_name));
   }
   
   @Override
   public String[] getArgumentNames() {
     return _argNames;
   }
   
   /**
    *
    * @return
    */
   @Override
   public Value invoke(final ExecutionContext ec) {
      try {
         _body.accept(ec.getStatementVisitor());
         return Undefined.UNDEFINED;
      }
      catch (final FlowReturnException ignored) {
         return ec.currentScope().getReturn();
      }
   }

   @Override
   public ActivationRecord parentScope() {
      return _parentAR;
   }
   
}
